package com.chromatic.competition.service;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.util.Log;

import com.chromatic.competition.R;

public class MusicService extends Service {
    private MediaPlayer mediaPlayer;

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        // 创建 MediaPlayer 对象并设置背景音乐资源
        mediaPlayer = MediaPlayer.create(this, R.raw.background_music);
        Log.d("TAG", "---On Create---:背景音乐服务启动");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // 播放背景音乐
        mediaPlayer.start();
        Log.d("TAG", "---On Start Command---:背景音乐服务启动");
        return START_STICKY; // 在服务被杀死后自动重启
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        // 停止播放背景音乐并释放 MediaPlayer 资源
        if (mediaPlayer != null) {
            mediaPlayer.stop();
            mediaPlayer.release();
            mediaPlayer = null;
        }
        Log.d("TAG", "---On Destroy---:背景音乐服务摧毁");
    }
}